<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Console Commands - Special Topics - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w434" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w434-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w434-collapse" class="collapse navbar-collapse"><ul id="w435" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w436" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-418" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-418" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-419" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-419" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-420" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-420" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item" href="#navigation-421" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-421" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-422" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-422" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-423" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-423" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-424" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-424" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-425" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-425" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-426" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-426" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-427" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-427" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item" href="#navigation-428" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-428" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-429" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-429" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-430" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-430" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item active" href="#navigation-431" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-431" class="submenu panel-collapse collapse in"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item active" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-432" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-432" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-433" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-433" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Console applications <span id="console-applications"></span><a href="#console-applications" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#usage">Usage</a></li>
<li><a href="#entry-script">The entry script</a></li>
<li><a href="#configuration">Configuration</a></li>
<li><a href="#create-command">Creating your own console commands</a></li></ol></div>
<p>Besides the rich features for building web applications, Yii also has full featured support for console applications
which are mainly used to create background and maintenance tasks that need to be performed for a website.</p>
<p>The structure of console applications is very similar to a Yii web application. It consists of one
or more <a href="./yii-console-controller.html">yii\console\Controller</a> classes, which are often referred to as "commands" in the console environment.
Each controller can also have one or more actions, just like web controllers.</p>
<p>Both project templates already have a console application with them.
You can run it by calling the <code>yii</code> script, which is located in the base directory of the repository. 
This will give you a list of available commands when you run it without any further parameters:</p>
<p><img src="images/tutorial-console-help.png" alt="Running ./yii command for help output" /></p>
<p>As you can see in the screenshot, Yii has already defined a set of commands that are available by default:</p>
<ul>
<li><a href="./yii-console-controllers-assetcontroller.html">AssetController</a> - Allows you to combine and compress your JavaScript and CSS files.
You can learn more about this command in the <a href="guide-structure-assets.html#using-the-asset-command">Assets Section</a>.</li>
<li><a href="./yii-console-controllers-cachecontroller.html">CacheController</a> - Allows you to flush application caches.</li>
<li><a href="./yii-console-controllers-fixturecontroller.html">FixtureController</a> - Manages fixture data loading and unloading for testing purposes.
This command is described in more detail in the <a href="guide-test-fixtures.html#managing-fixtures">Testing Section about Fixtures</a>.</li>
<li><a href="./yii-console-controllers-helpcontroller.html">HelpController</a> - Provides help information about console commands, this is the default command
and prints what you have seen in the above output.</li>
<li><a href="./yii-console-controllers-messagecontroller.html">MessageController</a> - Extracts messages to be translated from source files.
To learn more about this command, please refer to the <a href="guide-tutorial-i18n.html#message-command">I18N Section</a>.</li>
<li><a href="./yii-console-controllers-migratecontroller.html">MigrateController</a> - Manages application migrations.
Database migrations are described in more detail in the <a href="guide-db-migrations.html">Database Migration Section</a>.</li>
<li><a href="./yii-console-controllers-servecontroller.html">ServeController</a> - Allows you run PHP built-in web server.</li>
</ul>
<h2>Usage  <span id="usage"></span><a href="#usage" class="hashlink">&para;</a></h2><p>You execute a console controller action using the following syntax:</p>
<pre><code class="hljs apache"><span class="hljs-keyword">yii</span> &lt;route&gt;<span class="hljs-sqbracket"> [--option1=value1 --option2=value2 ... argument1 argument2 ...]</span>
</code></pre>
<p>In the above, <code>&lt;route&gt;</code> refers to the route to the controller action. The options will populate the class
properties and arguments are the parameters of the action method.</p>
<p>For example, the <a href="./yii-console-controllers-basemigratecontroller.html#actionUp()-detail">MigrateController::actionUp()</a>
with <a href="./yii-console-controllers-migratecontroller.html#$migrationTable-detail">MigrateController::$migrationTable</a> set to <code>migrations</code>
and a limit of 5 migrations can be called like so:</p>
<pre><code class="hljs nginx"><span class="hljs-title">yii</span> migrate/up <span class="hljs-number">5</span> --migrationTable=migrations
</code></pre>
<blockquote class="note"><p><strong>Note: </strong>When using <code>*</code> in console, don't forget to quote it as <code>"*"</code> in order to avoid executing it as a shell
glob that will be replaced by all file names of the current directory.</p>
</blockquote>
<h2>The entry script  <span id="entry-script"></span><a href="#entry-script" class="hashlink">&para;</a></h2><p>The console application entry script is equivalent to the <code>index.php</code> bootstrap file used for the web application.
The console entry script is typically called <code>yii</code>, and located in your application's root directory.
It contains code like the following:</p>
<pre><code class="hljs php language-php"><span class="hljs-comment">#!/usr/bin/env php</span>
<span class="hljs-preprocessor">&lt;?php</span>
<span class="hljs-comment">/**
 * Yii console bootstrap file.
 */</span>

defined(<span class="hljs-string">'YII_DEBUG'</span>) <span class="hljs-keyword">or</span> define(<span class="hljs-string">'YII_DEBUG'</span>, <span class="hljs-keyword">true</span>);

<span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/vendor/autoload.php'</span>);
<span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/vendor/yiisoft/yii2/Yii.php'</span>);

<span class="hljs-variable">$config</span> = <span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/config/console.php'</span>);

<span class="hljs-variable">$application</span> = <span class="hljs-keyword">new</span> yii\console\Application(<span class="hljs-variable">$config</span>);
<span class="hljs-variable">$exitCode</span> = <span class="hljs-variable">$application</span>-&gt;run();
<span class="hljs-keyword">exit</span>(<span class="hljs-variable">$exitCode</span>);
</code></pre>
<p>This script will be created as part of your application; you're free to edit it to suit your needs. The <code>YII_DEBUG</code> constant can be set to <code>false</code> if you do
not want to see a stack trace on error, and/or if you want to improve the overall performance. In both basic and advanced application
templates, the console application entry script has debugging enabled by default to provide a more developer-friendly environment.</p>
<h2>Configuration  <span id="configuration"></span><a href="#configuration" class="hashlink">&para;</a></h2><p>As can be seen in the code above, the console application uses its own configuration file, named <code>console.php</code>. In this file
you should configure various <a href="guide-structure-application-components.html">application components</a> and properties for the console application in particular.</p>
<p>If your web application and console application share a lot of configuration parameters and values, you may consider moving the common
parts into a separate file, and including this file in both of the application configurations (web and console).
You can see an example of this in the "advanced" project template.</p>
<blockquote class="tip"><p><strong>Tip: </strong>Sometimes, you may want to run a console command using an application configuration that is different
from the one specified in the entry script. For example, you may want to use the <code>yii migrate</code> command to
upgrade your test databases, which are configured in each individual test suite. To change the configuration
dynamically, simply specify a custom application configuration
file via the <code>appconfig</code> option when executing the command:</p>
<pre><code class="hljs nginx"><span class="hljs-title">yii</span> &lt;route&gt; --appconfig=path/to/config.php ...
</code></pre>
</blockquote>
<h2>Creating your own console commands  <span id="create-command"></span><a href="#create-command" class="hashlink">&para;</a></h2><h3>Console Controller and Action <span id="console-controller-and-action"></span><a href="#console-controller-and-action" class="hashlink">&para;</a></h3><p>A console command is defined as a controller class extending from <a href="./yii-console-controller.html">yii\console\Controller</a>. In the controller class,
you define one or more actions that correspond to sub-commands of the controller. Within each action, you write code that implements the appropriate tasks for that particular sub-command.</p>
<p>When running a command, you need to specify the route to the  controller action. For example,
the route <code>migrate/create</code> invokes the sub-command that corresponds to the
<a href="./yii-console-controllers-basemigratecontroller.html#actionCreate()-detail">MigrateController::actionCreate()</a> action method.
If a route offered during execution does not contain an action ID, the default action will be executed (as with a web controller).</p>
<h3>Options <span id="options"></span><a href="#options" class="hashlink">&para;</a></h3><p>By overriding the <a href="./yii-console-controller.html#options()-detail">yii\console\Controller::options()</a> method, you can specify options that are available
to a console command (controller/actionID). The method should return a list of the controller class's public properties.
When running a command, you may specify the value of an option using the syntax <code>--OptionName=OptionValue</code>.
This will assign <code>OptionValue</code> to the <code>OptionName</code> property of the controller class.</p>
<p>If the default value of an option is of an array type and you set this option while running the command,
the option value will be converted into an array by splitting the input string on any commas.</p>
<h3>Options Aliases <span id="options-aliases"></span><a href="#options-aliases" class="hashlink">&para;</a></h3><p>Since version 2.0.8 console command provides <a href="./yii-console-controller.html#optionAliases()-detail">yii\console\Controller::optionAliases()</a> method to add
aliases for options.</p>
<p>To define an alias, override <a href="./yii-console-controller.html#optionAliases()-detail">yii\console\Controller::optionAliases()</a> in your controller, for example:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">commands</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">console</span>\<span class="hljs-title">Controller</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span>
</span>{
    <span class="hljs-keyword">public</span> <span class="hljs-variable">$message</span>;
    
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">options</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">return</span> [<span class="hljs-string">'message'</span>];
    }
    
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">optionAliases</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">return</span> [<span class="hljs-string">'m'</span> =&gt; <span class="hljs-string">'message'</span>];
    }
    
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionIndex</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-variable">$message</span> . <span class="hljs-string">"\n"</span>;
    }
}
</code></pre>
<p>Now, you can use the following syntax to run the command:</p>
<pre><code class="hljs ">./yii hello -m=hello
</code></pre>
<h3>Arguments <span id="arguments"></span><a href="#arguments" class="hashlink">&para;</a></h3><p>Besides options, a command can also receive arguments. The arguments will be passed as the parameters to the action
method corresponding to the requested sub-command. The first argument corresponds to the first parameter, the second
corresponds to the second, and so on. If not enough arguments are provided when the command is called, the corresponding parameters
will take the declared default values, if defined. If no default value is set, and no value is provided at runtime, the command will exit with an error.</p>
<p>You may use the <code>array</code> type hint to indicate that an argument should be treated as an array. The array will be generated
by splitting the input string on commas.</p>
<p>The following example shows how to declare arguments:</p>
<pre><code class="hljs php language-php"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleController</span> <span class="hljs-keyword">extends</span> \<span class="hljs-title">yii</span>\<span class="hljs-title">console</span>\<span class="hljs-title">Controller</span>
</span>{
    <span class="hljs-comment">// The command "yii example/create test" will call "actionCreate('test')"</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionCreate</span><span class="hljs-params">(<span class="hljs-variable">$name</span>)</span> </span>{ ... }

    <span class="hljs-comment">// The command "yii example/index city" will call "actionIndex('city', 'name')"</span>
    <span class="hljs-comment">// The command "yii example/index city id" will call "actionIndex('city', 'id')"</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionIndex</span><span class="hljs-params">(<span class="hljs-variable">$category</span>, <span class="hljs-variable">$order</span> = <span class="hljs-string">'name'</span>)</span> </span>{ ... }

    <span class="hljs-comment">// The command "yii example/add test" will call "actionAdd(['test'])"</span>
    <span class="hljs-comment">// The command "yii example/add test1,test2" will call "actionAdd(['test1', 'test2'])"</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionAdd</span><span class="hljs-params">(array <span class="hljs-variable">$name</span>)</span> </span>{ ... }
}
</code></pre>
<h3>Exit Code <span id="exit-code"></span><a href="#exit-code" class="hashlink">&para;</a></h3><p>Using exit codes is a best practice for console application development. Conventionally, a command returns <code>0</code> to indicate that
everything is OK. If the command returns a number greater than zero, that's considered to be indicative of an error. The number returned will be the error
code, potentially usable to find out details about the error.
For example <code>1</code> could stand generally for an unknown error and all codes above would be reserved for specific cases: input errors, missing files, and so forth.</p>
<p>To have your console command return an exit code, simply return an integer in the controller action
method:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionIndex</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-comment">/* some problem */</span>) {
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"A problem occurred!\n"</span>;
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    }
    <span class="hljs-comment">// do something</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>There are some predefined constants you can use:</p>
<ul>
<li><a href="./yii-console-controller.html#EXIT_CODE_NORMAL-detail">Controller::EXIT_CODE_NORMAL</a> with value of <code>0</code>;</li>
<li><a href="./yii-console-controller.html#EXIT_CODE_ERROR-detail">Controller::EXIT_CODE_ERROR</a> with value of <code>1</code>.</li>
</ul>
<p>It's a good practice to define meaningful constants for your controller in case you have more error code types.</p>
<h3>Formatting and colors <span id="formatting-and-colors"></span><a href="#formatting-and-colors" class="hashlink">&para;</a></h3><p>Yii console supports formatted output that is automatically degraded to non-formatted one if it's not supported
by terminal running the command.</p>
<p>Outputting formatted strings is simple. Here's how to output some bold text:</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$this</span>-&gt;stdout(<span class="hljs-string">"Hello?\n"</span>, Console::BOLD);
</code></pre>
<p>If you need to build string dynamically combining multiple styles it's better to use <a href="./yii-helpers-baseconsole.html#ansiFormat()-detail">ansiFormat()</a>:</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$name</span> = <span class="hljs-variable">$this</span>-&gt;ansiFormat(<span class="hljs-string">'Alex'</span>, Console::FG_YELLOW);
<span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello, my name is $name."</span>;
</code></pre>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:29 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
